﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.Skins;
using DevExpress.LookAndFeel;
using DevExpress.UserSkins;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraBars.Helpers;
using CNotebook;

namespace NotebookS2
{
    public partial class FND_BANK_ACCOUNT : RibbonForm
    {
        public FND_BANK_ACCOUNT()
        {
            InitializeComponent();
        }

        public string ConnectionString { get; set; }

        public string SelectedBankCode { get; set; }
        public string SelectedBankName { get; set; }
        public string SelectedBranchCode { get; set; }
        public string SelectedBranchName { get; set; }
        public string SelectedAccountNumber { get; set; }
        public string SelectedGLAccountCode { get; set; }
        public string SelectedGLAccountName { get; set; }

        void BindByBranchCode(string BranchCode)
        {
            BankAccountManager oManager = new BankAccountManager();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();

            DataTable tbl = oManager.BrowseByBranchCode(BranchCode);

            oManager.Close();

            gridControl1.DataSource = tbl;
            gridControl1.Refresh();

        }

        void BindByBranchName(string BranchName)
        {
            BankAccountManager oManager = new BankAccountManager();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();

            DataTable tbl = oManager.BrowseByBranchName(BranchName);

            oManager.Close();

            gridControl1.DataSource = tbl;
            gridControl1.Refresh();
        }

        void BindByAccountNumber(string AccountNumber)
        {
            BankAccountManager oManager = new BankAccountManager();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();

            DataTable tbl = oManager.BrowseByAccountNumber(AccountNumber);

            oManager.Close();

            gridControl1.DataSource = tbl;
            gridControl1.Refresh();
        }

        private void FND_BANK_ACCOUNT_Load(object sender, EventArgs e)
        {
            cboSearch.SelectedIndex = 0;
        }

        private void txtSearch_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            int iSelectedIndex = cboSearch.SelectedIndex;

            switch (iSelectedIndex)
            {
                case 0:
                    BindByBranchCode(txtSearch.Text);
                    break;
                case 1:
                    BindByBranchName(txtSearch.Text);
                    break;
                case 2:
                    BindByAccountNumber(txtSearch.Text);
                    break;
            }
        }

        private void btnChoose_Click(object sender, EventArgs e)
        {
            int iFocusRowHandle = gridView1.FocusedRowHandle;


            this.SelectedBankCode = gridView1.GetRowCellValue(iFocusRowHandle, gridView1.Columns["BankCode"]).ToString();
            this.SelectedBankName = gridView1.GetRowCellValue(iFocusRowHandle, gridView1.Columns["BankName"]).ToString();
            
            this.SelectedBranchCode = gridView1.GetRowCellValue(iFocusRowHandle, gridView1.Columns["BranchCode"]).ToString();
            this.SelectedBranchName = gridView1.GetRowCellValue(iFocusRowHandle, gridView1.Columns["BranchName"]).ToString();

            this.SelectedGLAccountCode = gridView1.GetRowCellValue(iFocusRowHandle, gridView1.Columns["AcctCode"]).ToString();
            this.SelectedGLAccountName = gridView1.GetRowCellValue(iFocusRowHandle, gridView1.Columns["AcctName"]).ToString();
            this.SelectedAccountNumber = gridView1.GetRowCellValue(iFocusRowHandle, gridView1.Columns["AccountNumber"]).ToString();


            this.Close();
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            btnChoose_Click(sender, e);
        }

        private void gridView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnChoose_Click(sender, e);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
